package project.taobao.microsoft.transaction.provider.one.config;

import org.apache.activemq.command.ActiveMQObjectMessage;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.listener.DefaultMessageListenerContainer;
import project.taobao.commons.activemq.template.ActiveMqTemplate;
import project.taobao.commons.activemq.utils.ActiveMqUtils;
import org.springframework.jms.listener.DefaultMessageListenerContainer;
import project.taobao.commons.activemq.template.ActiveMqTemplate;
import project.taobao.commons.activemq.utils.ActiveMqUtils;

import javax.jms.*;
import java.util.ArrayList;
import java.util.List;

@Configuration
public class AcMqConfig {
    // methods
    @Bean
    public DefaultMessageListenerContainer getJContainer(){
        System.out.println("1");
        Destination destination = ActiveMqUtils.getDestination("123");
        DefaultMessageListenerContainer container = ActiveMqTemplate.getDefaultMessageListenerContainer(destination);
        container.setMessageListener(new MessageListener() {
            @Override
            public void onMessage(Message message) {
                try {
                    System.out.println(((TextMessage) message).getText());
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
        });
        return container;
    }

    @Bean
    public DefaultMessageListenerContainer getYContainer(){
        System.out.println("2");
        Destination destination = ActiveMqUtils.getDestination("queueDestination");
        DefaultMessageListenerContainer container = ActiveMqTemplate.getDefaultMessageListenerContainer(destination);
        container.setMessageListener(new MessageListener() {
            @Override
            public void onMessage(Message message) {
                try {

                    List<Double> list = (ArrayList)((ActiveMQObjectMessage)message).getObject();
                    list.forEach(s -> System.out.println(s));
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
        });
        return container;
    }
    // getters, setters

    // constructors
}
